<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * rsa处理类
 * @package  htdocs
 * @version 1.0
 */

class quanliRsa {

    private $private_key; //私钥

    private $public_key; //公钥

    private $third_public_key; //第三方公钥

    public function __construct( $private_key, $public_key, $third_public_key = null ) {
        $this->private_key = $private_key;
        $this->public_key = $public_key;
        $this->third_public_key = $third_public_key;
    }

    /**
     * 验签
     * @param string $decrypt_string 签名字符串
     * @param string $sign_string 签名
     * @return bool
     */
    public function decrypt( $decrypt_string, $sign_string ) {
        $pubKey = file_get_contents($this->third_public_key);
        $res = openssl_get_publickey($pubKey);
        $result = (bool)openssl_verify($decrypt_string, base64_decode($sign_string), $res);
        openssl_free_key($res);
        return $result;
    }

    /**
     * 生成签名
     * @param string $encrypt_string 签名字符串
     * @return string
     */
    public function encrypt( $encrypt_string ) {
        $priKey = file_get_contents($this->private_key);
        $res = openssl_get_privatekey($priKey);
        openssl_sign($encrypt_string, $sign_string, $res);
        openssl_free_key($res);
        $sign_string = base64_encode($sign_string);
        return $sign_string;
    }

    /**
     * 获得公钥
     * @return string
     */
    public function getPublicKey() {
        $pubKey = file_get_contents($this->public_key);
        return $pubKey;
    }

}